package com.atde._03_creational_design_patterns.fcatory.absfactory.pizzastore.order;


import com.atde._03_creational_design_patterns.fcatory.absfactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @BelongsProject: hsp_java_design_mode
 * @BelongsPackage: com.atde._03_creational_design_patterns.fcatory.absfactory.pizzastore.order
 * @Author: Del
 * @CreateTime: 2025-06-03  01:08
 * @Description: 抽象工厂实现 --- 实现层 --- 聚合抽象工厂
 * @Version: 1.0
 */
public class OrderPizza {
    AbsFactory absFactory;
    public OrderPizza(AbsFactory absFactory) {
        this.absFactory = absFactory;
        setAbsFactory(absFactory);
    }

    private void setAbsFactory(AbsFactory absFactory) {
        Pizza  pizza = null;
        String orderType = "";
        this.absFactory = absFactory;
        do {
            // 循环读取客户希望订购的披萨种类
            orderType = createPizzas();
            // 根据订购的种类，创建披萨
            // 此处运用了多态，具体执行的类，由抽象工厂具体实现类来决定
            pizza = absFactory.createPizza(orderType);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购披萨失败");
                break;
            }
        } while (true);
    }

    /***
     * @author: Del
     * @date: 2025/6/2 23:15
     * @return String 披萨的类型
     * @description:  用于获取订购披萨的类型
     */
    private String createPizzas() {
        try {
            BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("请输入披萨类型：");
            return buf.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
